package com.kai.prety.bluetooth.connet;

import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;


import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;



public class ConnetThread extends Thread {
	
	final String tag = "ConnectThread";
	
	final int PRETY_DATA_SIZE = 30;
	
	private ArrayBlockingQueue<PretyData> pretyDatas = new ArrayBlockingQueue<PretyData>(PRETY_DATA_SIZE); 
	
	private BluetoothAdapter bluetoothAdapter = null;
	private BluetoothDevice  bluetoothDevice  = null;
	private BluetoothSocket  bluetoothSocket  = null;
	private UUID uuid = null;
	
	public ConnetThread(BluetoothAdapter ba, BluetoothDevice bd, UUID uuid) {
		// TODO Auto-generated constructor stub
		this.bluetoothAdapter = ba;
		this.bluetoothDevice  = bd;
		this.uuid = uuid;
	}
	//search if there is Bluetooth using of Prety
	private int SearchPretyDevice()
	{
		
		return 0;
		
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		boolean reconnect = false;
		
		while(true)
		{
			if(reconnect)
			{
				Log.i(tag, "do not need connet");
				continue;
			}
			try {
				
				bluetoothSocket = this.bluetoothDevice.createRfcommSocketToServiceRecord(this.uuid);
				bluetoothSocket.connect();
				
				pretyDatas.clear();	//clear the default data;
				
				ReadThread readThread = new ReadThread(pretyDatas);
				WriteThread writeThread = new WriteThread(pretyDatas);
				
				//start the read and write thread
				readThread.start();
				writeThread.start();
				
				
				
			} catch (Exception e) {
				// TODO: handle exception
				
			}
		}
	}

}
